package com.clz.workorder.viewmodel

import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ObjectUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.OrderTypeBean
import com.czl.base.data.bean.UserBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2021/12/15 10:06
 */
class TransferOrderViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()
    val typeName = ObservableField("")
    val deptName = ObservableField("")
    val repairerName = ObservableField("")
    var repairType = ""
    var no = ""
    var memo = ""
    var repairerId = ""
    var subTypeId = ""
    var deptId = ""

    class UiChangeEvent {
        val queryTypeCompleteEvent: SingleLiveEvent<List<OrderTypeBean>> = SingleLiveEvent()
        val queryUserCompleteEvent: SingleLiveEvent<List<UserBean>> = SingleLiveEvent()
        val openTypePopEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val openUserPopEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val turnOrderEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    }

    val onCommentChangeCommand: BindingCommand<String> = BindingCommand(BindingConsumer {
        memo = it
    })

    val btnSubmitClick: BindingCommand<Any> = BindingCommand(BindingAction {
        turnRepair()
    })

    val onChoiceTypeClick: BindingCommand<Any> = BindingCommand(BindingAction {
        uc.openTypePopEvent.call()
    })
    val onChoiceUserClick: BindingCommand<Any> = BindingCommand(BindingAction {
        uc.openUserPopEvent.call()
    })

    private fun turnRepair() {
        if (ObjectUtils.isEmpty(subTypeId)) {
            showNormalToast("请选择维修类型")
            return
        }
        if (ObjectUtils.isEmpty(repairerId)) {
            showNormalToast("请选择维修人")
            return
        }
        model.turnRepair(
            mapOf(
                "memo" to memo,
                "no" to no,
                "repairerId" to repairerId,
                "subTypeId" to subTypeId
            )
        ).compose(RxThreadHelper.rxSchedulerHelper(this@TransferOrderViewModel))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    dismissLoading()
                    if (t.code == 200) {
                        uc.turnOrderEvent.call()
                        showSuccessToast(t.msg)
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    /**
     * 查询报修类型
     */
    fun queryWorkOrderType() {
        model.queryWorkOrderType(repairType,model.getAreaId())
            .compose(RxThreadHelper.rxSchedulerHelper(this@TransferOrderViewModel))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<OrderTypeBean>>>() {
                override fun onResult(t: BaseBean<List<OrderTypeBean>>) {
                    dismissLoading()
                    if (t.code == 200) {
                        uc.queryTypeCompleteEvent.postValue(t.data)
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }

    /**
     * 根据部门获取人员列表
     */
    fun getUsersByDeptId() {
        if (ObjectUtils.isEmpty(deptId)) {
            showNormalToast("请选择维修类型")
            return
        }
        model.getUsersByDeptId(model.getAreaId(), model.getLoginCompanyId().toString(), deptId)
            .compose(RxThreadHelper.rxSchedulerHelper(this@TransferOrderViewModel))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<UserBean>>>() {
                override fun onResult(t: BaseBean<List<UserBean>>) {
                    dismissLoading()
                    if (t.code == 200) {
                        uc.queryUserCompleteEvent.postValue(t.data)
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }
}